/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * http://www.yaqb.org/lookupReferences?lookup_input=1:5+1:1 (Post operation)
 * TODO: support mobile browser and search engine
 * TODO: hightlight current verse or keyword (based on topics)
 * TODO: chapter names
 * TODO: add sentinel word to increase google translation quality. 
 * TODO: allow user to personalize translation
 * TODO: use own transliteration engine, so we are free from so many errors in yaqb
 * LowTODO: add manzil list
 * LowTODO: red color for diacritic (xrce do this with image). Possible with overlaping text in IE.
 * NOT POSSIBLE: try out Google transliteration API (ta3reeb is only from en to ar)
 */
public class QurText extends BaseView implements EntryPoint {

	public static final String QURTEXT_INTRO = "Use sections of Quran (soQuran) to track your progress on reading The Holy Qur'an. There are 558 sections or rukus to read. When devising these Rukus the Ulema made sure that Rukus contained aayats of the same topic and theme. Learn more about <a href='http://www.intisaarul.netfirms.com/rukus_of_the_holy_quran.htm'>Ruku History</a>, <a href='http://www.scribd.com/doc/16691610'>Ruku List</a>, and <a href='http://code.google.com/p/qurtext/'>Open Source</a>. Report <a href='http://code.google.com/p/qurtext/issues/list'>problem</a>.";
	public static final String FOOTER="Partners: <a href='http://www.tanzil.info'>Tanzil.info</a> | <a href='http://www.versebyversequran.com'>Quran MP3</a>";
	private HeaderView headerView;
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final QurTextServiceAsync qurTextService = GWT
			.create(QurTextService.class);

    private DeckPanel mainDeck = new DeckPanel();
	private UserPresenter userPresenter;
	private NavigationView navigationView;
	private NavigationPresenter navigationPresenter;
	private MainPresenter mainPresenter;
	private SectionPresenter sectionPresenter;
	private SectionView sectionView;

	/**
	 * This is the GWT entry point method.
	 */
	public void onModuleLoad() {
		
		//creating all presenters
		mainPresenter = new MainPresenter();
		userPresenter = new UserPresenter();
		navigationPresenter = new NavigationPresenter();
		sectionPresenter = new SectionPresenter();
		
		//creating all views
		headerView=new HeaderView();
		navigationView=new NavigationView();
		sectionView=new SectionView();

		//connection all presenters with views and service
		mainPresenter.setMainView(this);
		mainPresenter.setUserPresenter(userPresenter);
		mainPresenter.setNavigationPresenter(navigationPresenter);
		mainPresenter.setSectionPresenter(sectionPresenter);
		
		userPresenter.setQurTextService(qurTextService);
		userPresenter.setMainPresenter(mainPresenter);
		userPresenter.setHeaderView(headerView);
		sectionPresenter.setQurTextService(qurTextService);
		sectionPresenter.setSectionView(sectionView);
		sectionPresenter.setMainPresenter(mainPresenter);
		
		navigationPresenter.setQurTextService(qurTextService);
		navigationPresenter.setNavigationView(navigationView);
		navigationPresenter.setMainPresenter(mainPresenter);

		headerView.setUserPresenter(userPresenter);
		sectionView.setMainPresenter(mainPresenter);
		navigationView.setMainPresenter(mainPresenter);
		navigationView.setNavigationPresenter(navigationPresenter);

		//loading presenters
		userPresenter.onUserPresenterLoad();
		navigationPresenter.onNavigationPresenterLoad();
		sectionPresenter.onSectionPresenterLoad();
		
		//loading views
		navigationView.onNavigationViewLoad();
		sectionView.onSectionViewLoad();
		headerView.onHeaderViewLoad();
	    
		//loading this view
		mainDeck.setWidth("100%");
	    mainDeck.setHeight("100");
	    mainDeck.add(navigationView.getPanel());
	    mainDeck.add(sectionView.getPanel());
	    mainDeck.showWidget(0);

 	    // Add it to the root panel.
	    RootPanel.get("maindiv").add(mainDeck);

	    History.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				Window.setTitle("QurText - Personal Quran Tracking Tool");
				String historyToken = event.getValue();
				mainPresenter.handleHistoryToken(historyToken);
			}
	    	
	    });
	}

	public void redirectUrl(String targetUrl) {
		Window.Location.replace(targetUrl);
	}

	public void alert(String message) {
		Window.alert(message);
	}

	public boolean isProduction() {
		return 	Window.Location.getHref().contains(".com");
	}

	public void showSectionPanel() {
		mainDeck.showWidget(1);
	}

	public void ShowJumpPanel() {
		mainDeck.showWidget(0);
	}

	public String getHistoryToken() {
		return History.getToken();
	}

	public void saveProgressCookie(String readCookie, String lastCookie) {
		Date forever=new Date(System.currentTimeMillis()+100000000);
		Cookies.setCookie("read", readCookie, forever);
		Cookies.setCookie("last",lastCookie,forever);
	}

	public void newHistoryItem(String newItem) {
		History.newItem(newItem);
	}

	public String getCurrentUrl() {
		return Window.Location.getHref();
	}

	public String getCookie(String name) {
		return Cookies.getCookie(name);
	}

	public void addProgressPanel(VerticalPanel juzPanel, String string) {
		RootPanel.get("maindiv").add(juzPanel);
	}

}
